# ==========================
# ===  General settings  ===
# ==========================
set-option -g mouse on

# Undercurl
set -g default-terminal "${TERM}"
set -as terminal-overrides ',*:Smulx=\E[4::%p1%dm'  # undercurl support
set -as terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'  # underscore colours - needs tmux-3.0
set -as terminal-overrides ",*256col*:RGB"

set -g history-limit 10000

set -s focus-events on

# Index starts from 1
set -g base-index 1
setw -g pane-base-index 1   # make pane numbering consistent with windows

setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on  # renumber windows when a window is closed

set -g set-titles on                        # set terminal title
set -g set-titles-string '#h ❐ #S ● #I #W'

# reduce the time when type prefix
set -s escape-time 0

setw -g monitor-activity on
set -g visual-activity off

set -g repeat-time 400

set -g allow-passthrough on
# ==========================
# ===    Colorscheme     ===
# ==========================
set -g message-style fg='colour222',bg='colour238'
set -g message-command-style fg='colour222',bg='colour238'

set-option -g status-style                 fg=colour15,bg=#282a36
set-option -g pane-border-style            fg=green,bg=#282a36
set-option -g pane-active-border-style     fg=yellow,bg=#282a36
set-option -g window-status-activity-style fg=colour111

# =====================================
# ===    Appearence and status bar  ===
# ======================================
source-file ~/dotfiles/tmux/tmux-line.conf

# ==========================
# ===   Key bindings     ===
# ==========================
setw -g xterm-keys on
# use vi mode
set -g mode-keys vi
# set prefix key
set -g prefix C-z
set -g prefix2 C-g
# unbind the default prefix key C-b
unbind C-b
unbind '"'
unbind %
bind | split-window -h -c "#{pane_current_path}"
bind '\' split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# switch window
# bind h select-pane -L
# bind j select-pane -D
# bind k select-pane -U
# bind l select-pane -R

# resize
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind -n 'M-h' if-shell "$is_vim" 'send-keys M-h' 'resize-pane -L 1'
bind -n 'M-j' if-shell "$is_vim" 'send-keys M-j' 'resize-pane -D 1'
bind -n 'M-k' if-shell "$is_vim" 'send-keys M-k' 'resize-pane -U 1'
bind -n 'M-l' if-shell "$is_vim" 'send-keys M-l' 'resize-pane -R 1'
bind-key -T copy-mode-vi M-h resize-pane -L 1
bind-key -T copy-mode-vi M-j resize-pane -D 1
bind-key -T copy-mode-vi M-k resize-pane -U 1
bind-key -T copy-mode-vi M-l resize-pane -R 1

# switch windows
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"

bind-key -T copy-mode-vi C-h select-pane -L
bind-key -T copy-mode-vi C-j select-pane -D
bind-key -T copy-mode-vi C-k select-pane -U
bind-key -T copy-mode-vi C-l select-pane -R

# reload the tmux config
bind r source-file ~/.tmux.conf \; display "Configuration Reloaded"

bind-key -T prefix v copy-mode
bind-key -T copy-mode-vi 'v' send -X begin-selection # start selecting text with "v"
bind-key -T copy-mode-vi L send-keys -X end-of-line
bind-key -T copy-mode-vi H send-keys -X  back-to-indentation

# Toggle popup window with Alt-f
bind-key -n -N 'Toggle popup window' M-f if-shell -F '#{==:#{session_name},popup}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E 'tmux attach-session -t popup || tmux new-session -s popup'
}
# ============================
# ===       Plugins        ===
# ============================
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-open'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

set -g @plugin 'fcsonline/tmux-thumbs'
set -g @thumbs-key c-f
set -g @thumbs-command 'echo -n {} | xclip -sel clip && tmux set-buffer -- {} && tmux display-message "Copied {}"'
set -g @thumbs-upcase-command 'echo -n {} | xclip -sel clip && tmux set-buffer -- {} && tmux paste-buffer && tmux display-message "Copied {}"'

# tmux-resurrect
set -g @resurrect-capture-pane-contents 'on'
# save vim session
set -g @resurrect-strategy-vim 'session'
# save neovim session
set -g @resurrect-strategy-nvim 'session'
# auto open tmux after reopen machine
# set -g @continuum-boot 'on'
# auto restore session after entering tmux
# set -g @continuum-restore 'on'

if "test ! -d ~/.tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm && ~/.tmux/plugins/tpm/bin/install_plugins'"

run '~/.tmux/plugins/tpm/tpm'
